//
//  TipsView.m
//  CarCare
//
//  Created by ileo on 14-8-12.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "TipsView.h"

#define TOPGAP 32

@interface TipsView()

@end

@implementation TipsView

-(id)initWithFrame:(CGRect)frame image:(UIImage *)image title:(NSString *)title lightTitles:(NSArray *)lightTitles cancelClick:(void (^)())clickCancel {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        
        int y = 0;
        if (clickCancel) {
            CGFloat btnY = 12;
            UIImage * cancelImage = ImageNamed(@"tipsViewCancelIcon");
            [Factory buttonWithCenter:CGPointMake(self.width - 12 - cancelImage.size.width / 2, btnY + cancelImage.size.height / 2) withImage:cancelImage click:clickCancel onView:self];
        }
        if (image) {
            UIImageView *imageV = [Factory imageViewWithFrame:CGRectMake(0, 0, image.size.width, image.size.height) image:image onView:self];
            imageV.center = CGPointMake(frame.size.width / 2, image.size.height / 2 + TOPGAP);
            y = imageV.bottom;
        }
        if ([title isNotNil]) {
            if (y == 0) {
                y += 20;
            }
            if (image) {
                y += 15 ;
            }
            
            UIFont *font = FONT_DEFAULT_Light(17);
            UILabel *label = [Factory labelWithFrame:CGRectMake(10, y + 5, frame.size.width - 20, 45) font:font text:title textColor:(image ? [Theme mainNavColor] : COLOR_HEXSTRING(@"#424242")) onView:self textAlignment:NSTextAlignmentCenter];
            NSDictionary *attributes = @{NSFontAttributeName:font};
            label.height = [title boundingRectWithSize:CGSizeMake(label.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height;
            
            label.numberOfLines = NSIntegerMax;
            y = label.bottom;
            y += 20;
            
            CGFloat gap = 18;
            if(image)[[[UIView viewWithFrame:CGRectMake(gap, y, self.width - 2 * gap, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:self];
            
            if(image)y += 20 * __MAIN_RATIO_375;
        }
        if (lightTitles) {
            
            UIFont *font = FONT_DEFAULT_Light(15);
            NSDictionary *attributes = @{NSFontAttributeName:font};
            y += (image ? ([title isNotNil] ? 0 : 10) : ([title isNotNil] ? 0 : 30 * __MAIN_RATIO_375));
            for (int i = 0 ; i < lightTitles.count; i++) {
                NSString *lightTitle = lightTitles[i];
                CGFloat labelX = ((image && [title isNotNil]) ? (28 * __MAIN_RATIO_375) : 15);
                UILabel *label = [Factory labelWithFrame:CGRectMake(labelX, y + 10 * __MAIN_RATIO_375, frame.size.width - labelX * 2, 30) font:font text:lightTitle textColor:((image && [title isNotNil]) ? COLOR_HEXSTRING(@"#aaaaaa") : COLOR_HEXSTRING(@"#707070")) onView:self textAlignment:((image && [title isNotNil]) ? NSTextAlignmentLeft : NSTextAlignmentCenter)];
                label.height = [lightTitle boundingRectWithSize:CGSizeMake(label.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height;
                
                label.numberOfLines = NSIntegerMax;
                label.tag = 10010 + i;
                y = label.bottom;
            }
            
            y += 10;
        }
        

        self.height = y + (((image && [title isNotNil]) ? 40 : 30)) * __MAIN_RATIO_375 + 40;
    }
    return self;
}
+(TipsView *)upgradeTipsWithTitle:(NSString *)title lightTitles:(NSArray *)lightTitles cancelClick:(void (^)())cancelClick
{
    TipsView *tips = [[TipsView alloc] initWithFrame:CGRectMake(0, 0, 324*__MAIN_RATIO_375, 240) image:[UIImage imageNamed:@"upgradeIcon"] title:title lightTitles:lightTitles cancelClick:cancelClick];
    
    return tips;
}
+ (TipsView *)commentTipsWithTitle:(NSString *)title lightTitles:(NSArray *)lightTitles cancelClick:(void (^)())cancelClick
{
    TipsView *tips = [[TipsView alloc] initWithFrame:CGRectMake(0, 0, 324*__MAIN_RATIO_375, 240) image:[UIImage imageNamed:@"commentTipsIcon"] title:title lightTitles:lightTitles cancelClick:cancelClick];
    
    return tips;
}
+(TipsView *)okTipsWithTitle:(NSString *)title lightTitles:(NSArray *)lightTitles{
    TipsView *tips = [[TipsView alloc] initWithFrame:CGRectMake(0, 0, 324*__MAIN_RATIO_375, 240) image:[UIImage imageNamed:@"tipsSucc"] title:title lightTitles:lightTitles cancelClick:nil];
    return tips;
}

+(TipsView *)warnTipsWithTitle:(NSString *)title lightTitles:(NSArray *)lightTitles{
    TipsView *tips = [[TipsView alloc] initWithFrame:CGRectMake(0, 0, 324*__MAIN_RATIO_375, 240) image:[UIImage imageNamed:@"tipsWarn"] title:title lightTitles:lightTitles cancelClick:nil];
    return tips;
}

+(TipsView *)voiceTipsWithTitle:(NSString *)title lightTitles:(NSArray *)lightTitles{
    TipsView *tips = [[TipsView alloc] initWithFrame:CGRectMake(0, 0, 324*__MAIN_RATIO_375, 240) image:[UIImage imageNamed:@"tipsVoice"] title:title lightTitles:lightTitles cancelClick:nil];
    return tips;
}

+(TipsView *)textTipsWithTitle:(NSString *)title lightTitles:(NSArray *)lightTitles{
    TipsView *tips = [[TipsView alloc] initWithFrame:CGRectMake(0, 0, 324*__MAIN_RATIO_375, 200) image:nil title:title lightTitles:lightTitles cancelClick:nil];
    return tips;
}
+(TipsView *)textSmallTipsWithTitle:(NSString *)title lightTitles:(NSArray *)lightTitles
{
    TipsView *tips = [[TipsView alloc] initWithFrame:CGRectMake(0, 0, 256, 200) image:nil title:title lightTitles:lightTitles cancelClick:nil];
    return tips;
}

+(UIView *)tipsNoDataInScreenView:(UIView *)screenV{ 
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 250)];
    UIImageView *ima = [Factory imageViewWithCenter:CGPointMake(100, 88) image:ImageNamed(@"no_data.png") onView:view];
    [Factory labelWithFrame:CGRectMake(0, ima.bottom, 200, 60) font:FONT_DEFAULT_Light(16) text:LOCSTRING(@"no_data") textColor:[UIColor lightGrayColor] onView:view textAlignment:NSTextAlignmentCenter];
    
    view.center = screenV.center;
    view.top -= 70;
    [screenV addSubview:view];
    
    return view;
}

+(UIView *)tipsNoRewardInScreenView:(UIView *)screenV{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenV.width, 250)];
    UIImageView *ima = [Factory imageViewWithCenter:CGPointMake(screenV.width/2, 88) image:ImageNamed(@"no_reward.png") onView:view];
    [Factory labelWithFrame:CGRectMake(0, ima.bottom, screenV.width, 20) font:FONT_DEFAULT_Bold(14) text:LOCSTRING(@"no_reward") textColor:[UIColor lightGrayColor] onView:view textAlignment:NSTextAlignmentCenter];
    view.center = (CGPoint){screenV.width/2,screenV.height/2};
    view.top += 10;
    [screenV addSubview:view];
    
    return view;
}

-(void)addLeftButton:(NSString *)left clickLeft:(void (^)())clickLeft rightButton:(NSString *)right clickRight:(void (^)())clickRight{
    CGFloat width = self.width / 2;
    CGFloat height = 40 ;
    
    UIButton *but1 = [ViewFactory buttonWithFrame:CGRectMake(0, self.bottom - height, width, height) title:left normalColor:[UIColor whiteColor] highlightColor:[[UIColor blackColor] colorWithAlphaComponent:0.2] disabledColor:[UIColor clearColor] inView:self action:clickLeft];
    but1.layer.cornerRadius = 0;
    [but1 setTitleColor:[Theme wordColor] forState:UIControlStateNormal];
    but1.layer.masksToBounds = NO;
    but1.titleLabel.font = FONT_DEFAULT_Light(16);
    
    [[[UIView viewWithFrame:CGRectMake(0, but1.top, but1.width, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:self];
    
    UIButton *but2 = [ViewFactory normalButtonWithFrame:CGRectMake( but1.right, but1.top, width, height) title:right inView:self action:clickRight];
    but2.layer.cornerRadius = 0;
    but2.layer.masksToBounds = NO;
    but2.titleLabel.font = FONT_DEFAULT_Light(16);
    
}

-(void)addOKButton:(NSString *)ok clickOK:(void (^)())clickOK{
    CGFloat height = 40 ;
    UIButton * btn = [ViewFactory normalButtonWithFrame:CGRectMake(0, self.bottom - height, self.width, height) title:ok inView:self action:clickOK];
    btn.titleLabel.font = FONT_DEFAULT_Light(16);
    btn.layer.cornerRadius = 0;
    btn.layer.masksToBounds = NO;
}
- (void)addRedOkButton:(NSString *)ok clickOK:(void (^)())clickOK
{
    CGFloat height = 40 ;
    UIButton * btn = [ViewFactory buttonWithFrame:CGRectMake(0, self.bottom - height, self.width, height) title:ok normalColor:COLOR_HEXSTRING(@"f6735e") highlightColor:[COLOR_HEXSTRING(@"f6735e") colorWithAlphaComponent:0.7] disabledColor:COLOR_HEXSTRING(@"f6735e") inView:self action:clickOK];
    [btn setBackgroundColor:COLOR_HEXSTRING(@"f6735e")];
    btn.titleLabel.font = FONT_DEFAULT_Light(16);
    btn.layer.cornerRadius = 0;
    btn.layer.masksToBounds = NO;
}
@end
